package com.cgwx.service.impl;

import com.cgwx.entity.Download;
import com.cgwx.mapper.DownloadMapper;
import com.cgwx.service.IDownloadService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

@Service
public class DownloadServiceImpl implements IDownloadService {

    @Resource
    private DownloadMapper downloadMapper;

    @Override
    public String insertDownload(String account, String entityName, String location) {
        Download download = new Download();
        String id = generateId();
        download.setAccount(account);
        download.setStatus("0");
        download.setLocation(location);
        download.setEntityName(entityName);
        download.setId(id);
        download.setTime(Timestamp.valueOf(LocalDateTime.now()));
        downloadMapper.insertDownload(download);
        return id;
    }

    @Override
    public List<Download> getDownload(String account, String pattern, String column, String sort) {
        return downloadMapper.getDownload(account, pattern, column, sort);
    }

    @Override
    public String deleteDownload(String id) {
        boolean result = downloadMapper.deleteDownload(id);
        if (result) return "删除成功!";
        else return "删除失败!";
    }

    @Override
    public boolean updateDownload(String id, String url, String status) {
        return downloadMapper.updateDownload(id, url, status);
    }

    @Override
    public String generateId() {
        return UUID.randomUUID().toString();
    }
}
